package pers.mine.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pers.mine.dao.MemberMapper;
import pers.mine.po.Member;
import pers.mine.po.MemberExample;
import pers.mine.service.inter.MemberService;
import pers.mine.vo.PageInfo;

/**
 * @author Mine
 * @since 2016年10月20日_下午9:28:27
 */
@Service
public class MemberServiceImpl implements MemberService {
	@Autowired
	private MemberMapper mm;

	/*
	 * @see pers.mine.service.inter.MemberService#login(int, java.lang.String)
	 */
	@Override
	public Member login(int id, String pw) {
		// 验证是否为空
		if (id == 0 || pw == null || "".equals(pw.trim())) {
			return null;
		}
		// 去空格
		pw = pw.trim();
		// 执行查询
		Member one = mm.selectByPrimaryKey(id);
		// 验证密码是否相同,以及账户是否可用
		if (one != null && pw.equals(one.getPw()) && one.getState() == 1) {
			return one;
		}
		return null;
	}

	/*
	 * @see pers.mine.service.inter.MemberService#register(pers.mine.po.Member)
	 */
	@Override
	public boolean register(Member one) {
		// 简单验证
		if (one == null || one.getId() == null || one.getName() == null
				|| one.getPw() == null) {
			return false;
		}
		// 使用动态插入语句
		if (mm.insertSelective(one) == 1) {
			return true;
		}
		return false;
	}

	/*
	 * @see pers.mine.service.inter.MemberService#validate(int)
	 */
	@Override
	public boolean validate(int id) {
		if (id == 0)
			return false;
		return mm.selectByPrimaryKey(id) == null ? true : false;
	}

	/*
	 * @see
	 * pers.mine.service.inter.MemberService#editMemberInfo(pers.mine.po.Member)
	 */
	@Override
	public boolean editMemberInfo(Member one) {
		if (one == null) {
			return false;
		}
		// 设置状态为null 表示不修改状态
		one.setState(null);
		// 使用动态更新语句
		if (mm.updateByPrimaryKeySelective(one) == 1) {
			return true;
		}
		return false;
	}

	/*
	 * @see pers.mine.service.inter.MemberService#setMemberState(int, int)
	 */
	@Override
	public boolean setMemberState(Integer mid, Integer mstate) {
		// 简单验证合法性
		if (mid == null || mid <= 0) {
			return false;
		}
		if (mstate == null
				|| (mstate.intValue() != 1 && mstate.intValue() != 0)) {
			// mstate 只能是0或1
			return false;
		}
		Member one = new Member();
		one.setId(mid);
		one.setState(mstate);
		// 使用动态更新语句
		if (mm.updateByPrimaryKeySelective(one) == 1) {
			return true;
		}
		return false;
	}

	/*
	 * @see pers.mine.service.inter.MemberService#setMemberPrice(int, double)
	 */
	@Override
	public boolean setMemberBalance(int mid, double balance) {
		// 简单验证合法性
		if (mid <= 0 || balance < 0) {
			return false;
		}
		Member one = new Member();
		one.setId(mid);
		one.setBalance(balance);
		// 使用动态更新语句
		if (mm.updateByPrimaryKeySelective(one) == 1) {
			return true;
		}
		return false;
	}

	/*
	 * @see pers.mine.service.inter.MemberService#getOneMemberByID(int)
	 */
	@Override
	public Member getOneMemberByID(int mid) {
		// 简单验证合法性
		if (mid <= 0) {
			return null;
		}
		return mm.selectByPrimaryKey(mid);
	}

	/*
	 * @see
	 * pers.mine.service.inter.MemberService#findPageByExample(pers.mine.po.
	 * MemberExample)
	 */
	@Override
	public PageInfo<Member> findPageByExample(MemberExample example) {

		List<Member> ls = mm.selectByExample(example);
		long allSum = mm.getFindRows();

		Integer nowPage = null;
		Integer pageSize = null;
		if (example != null) {
			nowPage = example.getNowPage();
			pageSize = example.getPageSize();
		}
		// 考虑到显示全部查询记录,不分页
		if (nowPage == null) {
			nowPage = 1;
		}
		if (pageSize == null) {
			pageSize = (int) allSum;
		}

		PageInfo<Member> pi = new PageInfo<Member>((int) allSum, nowPage,
				pageSize);
		pi.setBasePageList(ls);

		return pi;
	}
}
